const koa = require('koa')
const http = require('http')
const router = require('koa-router')()
const sendFile = require('koa-sendfile')

const app = koa()

const server = http.createServer(app.callback())
const io = require('socket.io')(server)

router.get('/', function* () {
  yield sendFile(this, 'index.html')
})

app.use(router.routes())

io.on('connection', (socket) => {
  io.emit('chat message', {some: 'welcome the new user', from: socket.id})
  socket.on('disconnect', function(){
    io.emit('chat message', {some: `The user gone`, from: socket.id})
  })
  socket.on('chat message', function(msg) {
    io.emit('chat message', {some: msg, from: socket.id})
  })
})

server.listen(3003)